package com.itheima.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Province;
import com.itheima.service.ProvinceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/9/6
 */
@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
    private ProvinceService provinceService = new ProvinceService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Province> provinces = provinceService.selectAllProvince();
        String jsonStr = JSON.toJSONString(provinces);
        resp.setContentType("application/josn;charset=utf-8");
        resp.getWriter().write(jsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
